package Hxw3DEngine.asset.loader
{
	//import org.hamcrest.object.nullValue;

	public class AssetQueueLoader
	{
		
		private var res:Array;
		
		public var progress:Function;
		
		public var complete:Function;
		
		//加载头
		private var index:int = 0;
		//一波加载的个数
		public var count:int = 2;
		//当前已经加载成功的个数
		public var completeCount:int = 0;
		//是否在加载中
		private var isLoding:Boolean = false;
		
		/**
		 * @param _res				资源数组一个URL列表
		 * @param _count			并发下载数
		 * @param _progress			通知进度会有两个参数，一个当前一个总值
		 * @param _complete			完成回调
		 */		
		public function AssetQueueLoader(_res:Array,_complete:Function = null,_progress:Function = null,_count:int = 2)
		{
			res = _res; progress = _progress; complete = _complete;
		}
		
		//可以在下载时再加进来
		public function push(url:String):void{
			res.push(url);
			if(!isLoding)startLoad();
		}
		
		public function startLoad():void{
			if(res.length <=0 || isLoding) return ;
			isLoding = true;
			for (var i:int = index;i<index + count;i++){
				if(res[i]){
					var loader:AssetLoader = new AssetLoader();
					loader.load(res[i],onComplete);
				}
			}
		}
		
		private function onComplete():void{
			completeCount ++;
			index ++;
			if(completeCount >= res.length && Boolean(complete)) {
				complete();
				isLoding = false;
				dispose();
			}else if(completeCount % count == 0){
				isLoding = false;
				startLoad();
			}
		}
		
		private function dispose():void{
			return ;
			res = null;
			progress = null;
			complete = null;
		}
		
		
	}
}